<?php

namespace App\Http\Controllers\Admin\Promotion;

use App\Http\Models\Admin\Sys\SysParam;
use App\Http\Responses\ResponseDto;
use App\Services\Admin\EMailService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;

class EmailController extends Controller
{

    public function __construct(EMailService $service){
        $this->service = $service;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response{cate}/{subcategory}
     */
    public function index(Request $request)
    {
        return View::make('admin.sys.email')
            ->withMailconfig($this->service->getEmailConfig())
            ->withMailnotes(Config::get("common.email_notes"))
            ->withAssets($request['ASSETS']);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    public function testmail(Request $request)
    {

        $targe = array(
            "email"=>$request->input("email"),
            "name"=>$request->input("email")
        );
        $this->service->sendMail("email.test",$targe);
        return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $sysparam = new SysParam();
        $sysparam->name = Config::get('common.system_email_tag',"system_email_tag");
        $find = SysParam::where('name',$sysparam->name)->first();
        if($find){
            $sysparam = $find;
        }


        $val = array();
        foreach(Input::all() as $key=>$value){
            if($key == "ASSETS"){
                continue;
            }
            $val[$key] = $value;

        }
        $sysparam->value = serialize($val);

        if($sysparam->save()){
            return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
        }else{
            return ResponseDto::createResObj(trans("admin/message.server_fail"),ResponseDto::$FAIL);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

    }
}
